package com.um.jdy.platform.shop.controller;

import com.um.jdy.platform.shop.entity.dto.AftersaleDeliveryDTO;
import com.um.jdy.platform.shop.entity.dto.AftersaleRejectDTO;
import com.um.jdy.platform.shop.entity.dto.OrderAftersalePageDTO;
import com.um.jdy.platform.shop.entity.vo.OrderAftersaleItemVO;
import com.um.jdy.platform.shop.manager.OrderAftersaleManager;
import com.um.jdy.common.shop.service.OrderAftersaleService;
import com.um.jdy.common.shop.entity.po.OrderAftersale;
import com.um.jdy.common.shop.entity.dto.OrderAftersaleDTO;
import com.um.jdy.common.shop.entity.vo.OrderAftersaleVO;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.TokenRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BasePlusController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.ListDTO;
import com.um.springboot.starter.orm.params.PageDTO;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 控制器：订单售后表
 */
@RestController
@RequestMapping("/api/v1/cms/shop/order_aftersale")
@Api(tags = "OrderAftersaleController", description = "订单店铺售后表")
@ApiPermission("aftersale")
@TokenRequired
public class OrderAftersaleController extends BasePlusController<OrderAftersaleManager,OrderAftersaleService,OrderAftersale,OrderAftersaleDTO,OrderAftersaleVO, PageDTO, ListDTO> {

    @ApiPermission("page")
    @ApiOperation("分页查询订单")
    @PostMapping("get_page")
    public Response<Page<OrderAftersaleItemVO>> getPage(@RequestBody OrderAftersalePageDTO params) {
        PageParams pageParams = params.toPageParams(
                new String[]{"status","type","aftersaleNo"},
                new String[]{"a.status","a.type","a.aftersale_no"});
        return Response.success(manager.getPage(pageParams));
    }

    /**
     * 查询售后订单详情
     * @param id
     * @return
     */
    @ApiPermission("get")
    @ApiOperation("查询售后订单详情")
    @GetMapping("view/{id}")
    public Response getAftersale(@PathVariable String id){
        return Response.success(manager.getAftersale(id));
    }

    /**
     * 同意售后
     * @param id
     * @return
     */
    @ApiPermission("agree")
    @ApiOperation("同意售后")
    @PutMapping("agree/{id}")
    public Response agree(@PathVariable String id){
        return Response.success(manager.agree(id));
    }

    /**
     * 拒绝售后
     * @param rejectDTO
     * @return
     */
    @ApiPermission("reject")
    @ApiOperation("拒绝售后")
    @PutMapping("reject")
    public Response rejectAftersale(@RequestBody @Valid AftersaleRejectDTO rejectDTO){
        return Response.success(manager.rejectAftersale(rejectDTO.getId(),rejectDTO.getRejectReason()));
    }

    /**
     * 售后退款
     * @param id
     * @return
     */
    @ApiPermission("refund")
    @ApiOperation("售后退款")
    @PutMapping("refund/{id}")
    public Response refundAftersale(@PathVariable String id){
        return Response.success(manager.refundAftersale(id));
    }


    /**
     * 售后发货
     * @param deliveryDTO
     * @return
     */
    @ApiPermission("delivery")
    @ApiOperation("售后发货")
    @PostMapping("delivery")
    public Response deliveryAftersale(@RequestBody @Valid AftersaleDeliveryDTO deliveryDTO){
        return Response.success(manager.deliveryAftersale(deliveryDTO.getAftersaleOrderId(),deliveryDTO.getExpress(),deliveryDTO.getExpressNo()));
    }

}
